import React from 'react';
import PropTypes from 'prop-types';
import { Form, Modal, Row } from 'antd';
import dealSelect from './dealSelect';
import dealInput from './dealInput';

const formItemLayout = {
    labelCol: {
        span: 5
    },
    wrapperCol: {
        span: 18
    },
};

const Dialog = ({ item = {}, onCreate, form, formData, ...modalProps}) => {
    const { validateFields, getFieldsValue } = form;
    const handleOk = () => {
        validateFields((err) => {
            if (err) {
                return;
            }
            const data = getFieldsValue();
            onCreate(data)
        })
    };

    const modalOpts = {
        ...modalProps,
        onOk: handleOk
    };

    return (
        <Modal {...modalOpts}>
            <Form layout="horizontal">
                {
                    formData.map((e,index) => (
                        <Row key={index}>
                            {
                                e.isSelect ? dealSelect(e,form,formItemLayout,item) : dealInput(e,form,formItemLayout,item)
                            }
                        </Row>

                    ))
                }
            </Form>
        </Modal>
    )
};

Dialog.propTypes = {
    form: PropTypes.object.isRequired,
    type: PropTypes.string,
    item: PropTypes.object,
    onOk: PropTypes.func,
};

export default Form.create()(Dialog);